#!/usr/bin/env bash

set -o errexit

if [[ -t 1 ]]
then
    exec 2>&1 | ts '%X %Z'
fi

# This is to help with disk space monitoring - run "df" before and after
echo "Disk space when RunExport starts:" ; df -m
trap 'echo "Disk space when RunExport ends:" ; df -m' 0

MB_SERVER_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)
cd "$MB_SERVER_ROOT"

source admin/config.sh

. ./admin/functions.sh
make_temp_dir

set +o errexit
WITH_FULL_EXPORT=$1 ; shift
set -o errexit

# Make a date/time stamp for the full export
if [ "$WITH_FULL_EXPORT" ]
then
    EXPORT_STAMP=`TZ=UTC date +'%Y%m%d-%H%M%S'`
    EXPORT_ARG="--with-full-export"
else
    EXPORT_ARG="--without-full-export"
fi

# Create necessary directories and set permissions.  If we're not doing a full
# export then $EXPORT_STAMP is empty, so that argument is ignored.

mkdir -m "$BACKUP_DIR_MODE" -p \
    "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP" \
    "$BACKUP_DIR"/replication/
chown "$BACKUP_USER:$BACKUP_GROUP" \
    "$BACKUP_DIR"/fullexport/ \
    "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP" \
    "$BACKUP_DIR"/replication

mkdir -m "$FTP_DIR_MODE" -p \
    "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP" \
    "$FTP_DATA_DIR"/replication
chown "$FTP_USER:$FTP_GROUP" \
    "$FTP_DATA_DIR"/fullexport/ \
    "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP" \
    "$FTP_DATA_DIR"/replication

echo Making database snapshot
./admin/ExportAllTables \
    $EXPORT_ARG \
    --with-replication \
    --output-dir "$TEMP_DIR" \
    --compress \
    --replication-callback "$MB_SERVER_ROOT/admin/CopyReplication $TEMP_DIR" \
    --database MAINTENANCE \
    || exit $?

if [ "$WITH_FULL_EXPORT" ]
then
    # The remaining files are the full database export, including the
    # private data.
    # Copy the full export to the backup directory
    chown "$BACKUP_USER:$BACKUP_GROUP" "$TEMP_DIR"/*
    chmod "$BACKUP_FILE_MODE" "$TEMP_DIR"/*
    cp -a "$TEMP_DIR"/* "$BACKUP_DIR"/fullexport/"$EXPORT_STAMP"/

    # Now remove the "private" file(s)
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2.asc
    rm -rf "$TEMP_DIR"/mbdump-private.tar.bz2.gpg

    # and move the rest to the FTP directory
    chown "$FTP_USER:$FTP_GROUP" "$TEMP_DIR"/*
    chmod "$FTP_FILE_MODE" "$TEMP_DIR"/*
    mv "$TEMP_DIR"/* "$FTP_DATA_DIR"/fullexport/"$EXPORT_STAMP"/

    # Finally create a "latest-is" file, indicating the export we just did.
    rm -rf "$FTP_DATA_DIR"/fullexport/latest-is-*
    > "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"
    chmod "$FTP_FILE_MODE" "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"
    chown "$FTP_USER:$FTP_GROUP" "$FTP_DATA_DIR"/fullexport/latest-is-"$EXPORT_STAMP"

    # Finally finally, create a LATEST file whose *contents* are this export's tag
    rm -rf "$FTP_DATA_DIR"/fullexport/LATEST
    echo "$EXPORT_STAMP" > "$FTP_DATA_DIR"/fullexport/LATEST
    chmod "$FTP_FILE_MODE" "$FTP_DATA_DIR"/fullexport/LATEST
    chown "$FTP_USER:$FTP_GROUP" "$FTP_DATA_DIR"/fullexport/LATEST

    ./bin/delete-old-fullexports -k -r "$BACKUP_DIR"/fullexport "$FTP_DATA_DIR"/fullexport
    ./bin/rsync-fullexport-files
fi

# eof
